package com.jie.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.aop.OperationLogAnnotation;
import com.jie.entity.Airport;
import com.jie.listnenr.EasyExcelReadListener;
import com.jie.mapper.AirportMapper;
import com.jie.service.AirportService;
import com.jie.service.impl.AirportServiceImpl;
import com.jie.utils.R;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 小杰说java
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/airport")
@CrossOrigin //跨域
@Slf4j
public class AirportController {
    @Autowired
    private AirportService asc;
    @Autowired
    AirportServiceImpl airportService;
    @Autowired
    private AirportMapper airportMapper;

    private static String p = "dpt_cty_cd,";

    @OperationLogAnnotation(value = "过滤查询")
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "page", value = "当前页码", required = true)
                                  @PathVariable Long page,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true)
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) Airport airport
    ) {
        Page<Airport> pageParam = new Page<>(page, limit);
        QueryWrapper<Airport> wrapper = new QueryWrapper<>();
        //航空公司
        String fltNbr = airport.getCarrCd();
        //出发地点
        String arrvCtyCd = airport.getDptCtyCd();
        //到达地点
        String dptAirptCd = airport.getArrvCtyCd();
        //航班号
        String fl = airport.getFltNbr();
        //到达时间
        String flt = airport.getFltSegArrvDt();
        //航空公司
        if (!StringUtils.isEmpty(fltNbr) || fltNbr != null) {
            wrapper.eq("carr_cd", fltNbr);
        }
        //出发地点
        if (!StringUtils.isEmpty(arrvCtyCd) || arrvCtyCd != null) {
            wrapper.eq("dpt_cty_cd", arrvCtyCd);
        }
        //到达地点
        if (!StringUtils.isEmpty(dptAirptCd) || dptAirptCd != null) {
            wrapper.eq("arrv_cty_cd", dptAirptCd);
        }
        //航班号
        if (!StringUtils.isEmpty(fl) || fl != null) {
            wrapper.eq("flt_nbr", fl);
        }
        //到达时间
        if (!StringUtils.isEmpty(flt) || flt != null) {
            wrapper.eq("flt_seg_arrv_dt", flt);
        }
        asc.page(pageParam, wrapper);
        long total = pageParam.getTotal();
        List<Airport> records = pageParam.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    @OperationLogAnnotation(value = "列表")
    @GetMapping("page/{page}/{limit}")
    public R pageLimitAirport(@ApiParam(name = "page", value = "当前页码", required = true)
                              @PathVariable Long page,
                              @ApiParam(name = "limit", value = "每页记录数", required = true)
                              @PathVariable Long limit) {

        return asc.limit(page, limit);

    }

    /**
     * 根据id查询
     */
    @OperationLogAnnotation(value = "根据id查询")
    @GetMapping("getId/{id}")
    public R getId(@PathVariable String id) {
        Airport byId = asc.getById(id);
        return R.ok().data("id", byId);
    }

    /**
     * 修改航班信息
     *
     * @param airport
     * @return
     */
    @OperationLogAnnotation(value = "修改航班信息")
    @PostMapping("updateId")
    public R updateHangBan(@RequestBody Airport airport) {
        boolean id = asc.updateById(airport);
        if (id) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 新增航班
     *
     * @param airport
     * @return
     */
    @OperationLogAnnotation(value = "新增航班")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "AirPort", value = "航班对象", required = true)
            @RequestBody Airport airport) {

        asc.save(airport);
        return R.ok();
    }

    /**
     * 统计：旅客总量，航班总量 每个月的y基准价，和
     * 主航位总量(这个是主舱位是F和C的旅客量)
     */
    @OperationLogAnnotation(value = "统计")
    @RequestMapping("/getPuer")
    public R getPuer() {
        List<Map<String, Object>> puer = airportService.getPuer();
        return R.ok().data("puer", puer);
    }

    /**
     * 旅客量，航班量 每个月的y基准价，和
     * 主航位总量（5月到10月的都得求出 count()函数求数量即可）
     *
     * @return
     */
    @OperationLogAnnotation(value = "旅客量")
    @GetMapping("/getEryi")
    public R getEryi() {
        List<Map<String, Object>> eryi = airportService.getEryi();
        return R.ok().data("eryi", eryi);

    }

    /**
     * 5到10月的每个月游客量
     *
     * @return
     */
    @OperationLogAnnotation(value = "5到10月的每个月游客量")
    @RequestMapping("/getMonthlyPassenger")
    public R getMonthlyPassenger() {
        List<Map<String, Object>> mapList = airportService.getMonthlyPassenger();

        return R.ok().data("mapList", mapList);
    }

    /**
     * 航班每月数量
     *
     * @return
     */
    @OperationLogAnnotation(value = "航班每月数量")
    @GetMapping("/mFlights")
    public R getTotalMFlights() {
        List<Map<String, Object>> mFlights = airportService.getTotalMFlights();
        return R.ok().data("mFlights", mFlights);

    }

    /**
     * 前十热门城市
     *
     * @return
     */
    @OperationLogAnnotation(value = "前十热门城市")
    @RequestMapping("/getTopCities")
    public R getTopCities() {
        List<Map<String, Double>> topCitiess = airportService.getTopCitiess();
        return R.ok().data("topCities", topCitiess);
    }

    /**
     * 前三热门城市接口
     *
     * @return
     */
    @OperationLogAnnotation(value = "前三热门城市接口")
    @RequestMapping("/getTopCitiess")
    public R getTopCitiess() {
        List<List<Map<String, Object>>> topCities = airportService.getTopCities(3);
        //某天 某周 某月 某年的航班
        return R.ok().data("topCities", topCities);

    }

    /**
     * 统计每个月航班数量和旅客量
     *
     * @return
     */
    @GetMapping("/getStatistics")
    @OperationLogAnnotation(value = "统计每个月航班数量和旅客量")
    public R getStatisticsMonthlyFlights() {
        List<Map<String, Object>> list = airportService.getStatisticsMonthlyFlights();
        return R.ok().data("list", list);

    }


    /**
     * 统计每月航班总数 旅客量和每人平均分摊收入多少
     *
     * @return
     */
    @GetMapping("/total")
    @OperationLogAnnotation(value = "统计每月航班总数 旅客量和每人平均分摊收入多少")
    public R getTotalNumber() {
        List<Map<String, Object>> total = airportService.getTotalNumber();
        return R.ok().data("total", total);
    }

    /**
     * 统计每月航班总数 ,y基准价和每人平均分摊收入多少
     *
     * @param
     * @return
     * @throws IOException
     */
    @GetMapping("/get")
    public R get() {
        List<Map<String, Object>> list = airportService.get();

        return R.ok().data("list", list);
    }


    /**
     * 文件上传接口
     *
     * @param file
     * @return
     * @throws IOException
     */
    @OperationLogAnnotation(value = "文件上传接口")
    @PostMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Airport.class, new EasyExcelReadListener(airportMapper)).sheet().doRead();
        return "success";
    }

    /**
     * 飞机的日 周 月 总航班量
     *
     * @return
     */
    @OperationLogAnnotation(value = "飞机的日 周 月 总航班量")
    @GetMapping("/getFlightData")
    public R getFlightData() {
        List<Map> flightData = airportService.getFlightData();

        return R.ok().data("flightData", flightData);
    }

    /**
     * 每个月的客座率
     */
    @GetMapping("/passengerfive")
    @OperationLogAnnotation(value = "每个月的客座率")
    public R getPassengerFive() {
        List<Map<String, Object>> passengerFive = airportService.getPassengerFive();
        return R.ok().data("passengerfive", passengerFive);
    }

    @GetMapping("/getCustomXY")
    public R getCustomXY() {
        List<Map<String, Object>> list = airportService.getCustomXY();
        return R.ok().data("xy", list);
    }

    @GetMapping("/getAllFlights")
    public R getAllFlights() {
        List<Map<String, Object>> list = airportService.getAllFlights();
        return R.ok().data("flights", list);
    }

    @GetMapping("/getAllDpt")
    public R getAllDpt() {
        List<String> allDpt = airportService.getAllDpt();
        return R.ok().data("dpts", allDpt);
    }

    @PostMapping("/Sum")
    public R Sum(@RequestBody List<List<String>> dpts) {
        List<Map<String, Double>> sums = airportService.getSum(dpts);

        return R.ok().data("sums", sums);
    }

    @PostMapping("/averageValue")
    public R averageValue(@RequestBody List<List<String>> dpts) {

        List<Map<String, Double>> avg = airportService.getAVG(dpts);

        return R.ok().data("AVGs", avg);

    }

    @PostMapping("/getMax")
    public R getMax(@RequestBody List<List<String>> dpts) {
        List<Map<String, Double>> max = airportService.getMax(dpts);
        return R.ok().data("Maxs", max);
    }

    @PostMapping("/getMin")
    public R getMin(@RequestBody List<List<String>> dpts) {
        List<Map<String, Double>> min = airportService.getMin(dpts);
        return R.ok().data("Mins", min);
    }

    /**
     * 标准差
     *
     * @param dpts
     * @return
     */
    @PostMapping("/calculateVariance")
    public R calculateVariance(@RequestBody List<List<String>> dpts) {

        List<Map<String, Double>> variance = airportService.calculateVariance(dpts);

        return R.ok().data("Variances", variance);

    }

    /**
     * 计数
     *
     * @param dpts
     * @return
     */
    @PostMapping("/count")
    public R getCount(@RequestBody List<List<String>> dpts) {
        List<Map<String, Long>> count = airportService.getCount(dpts);

        return R.ok().data("Conuts", count);
    }

    //聚类
    @PostMapping("/Cluster")
    public R getCluster(@RequestBody List<List<String>> dpts) {
        List<Map<String, List>> cluster = airportService.getCluster(dpts);

        return R.ok().data("Cluster", cluster);
    }

    @PostMapping("/predict")
    public R predict(@RequestBody List<List<String>> dpts) {
        List<Map<String, Double>> predict = airportService.predict(dpts);
        return R.ok().data("predict", predict);
    }


}


